/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import au.com.bytecode.opencsv.CSVWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import model.TabellenEintrag;

/**
 *
 * @author Max
 */
public class CSVSaver {

    List<TabellenEintrag> liste;
    String filePath;

    public CSVSaver(ArrayList<TabellenEintrag> liste, String filePath) {
        this.liste = liste;
        this.filePath = filePath;
    }

    public void write() {
        File file = new File(filePath);
        if (file.exists() == false) {
            try {
            file.createNewFile();
            } catch (IOException exception) {
                System.out.println(exception.getMessage());
            }
        } 
        CSVWriter csvw = null;
        try {
            csvw = new CSVWriter(new FileWriter(file));
        } catch (IOException exception) {
            System.out.println(exception.getMessage());
        }
        if (csvw != null) {
            try {
                int i = 0;
                while (i < liste.size()) {
                    csvw.writeNext(liste.get(i).getEintrage());
                    i++;
                }
                csvw.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        

    }

}
